package com.ict.web.service.impl;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ict.web.domain.PerformanceIndicators;
import com.ict.web.domain.PerformanceRule;
import com.ict.web.domain.ProjectManagement;
import com.ict.web.dto.InsertPerformanceRuleDto;
import com.ict.web.dto.ScoreAddDto;
import com.ict.web.dto.ScoreEditDto;
import com.ict.web.mapper.PerformanceIndicatorsMapper;
import com.ict.web.mapper.PerformanceRuleMapper;
import com.ict.web.mapper.ProjectManagementMapper;
import com.ict.web.service.IPerformanceIndicatorsService;
import com.ict.web.service.IPerformanceRuleService;
import com.ict.web.service.IPerformanceService;
import com.ict.web.vo.PerIndicatorsVo;
import io.jsonwebtoken.lang.Collections;
import lombok.Data;
import lombok.val;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ict.web.mapper.ScoreMapper;
import com.ict.web.domain.Score;
import com.ict.web.service.IScoreService;

/**
 * 得分Service业务层处理
 *
 * @author ruoyi
 * @date 2022-02-14
 */
@Service
public class ScoreServiceImpl extends ServiceImpl<ScoreMapper, Score> implements IScoreService {

    @Autowired
    private IPerformanceService performanceService;
    @Autowired
    private ScoreMapper scoreMapper;

    public HashMap<String, Double> selectCloudResources(){
        // 设置 指标名称 （模拟值 --->  预备调用云资源接口）
        HashMap<String, Double> hashMap = new HashMap<>();
        hashMap.put("cpu利用率均值", 0.4025);
        hashMap.put("cpu利用率峰值", 0.2203);
        hashMap.put("内存利用率均值", 0.2003);
        hashMap.put("内存利用率峰值", 0.2503);
        hashMap.put("硬盘利用率均值", 0.2108);
        hashMap.put("硬盘利用率峰值", 0.3225);
        hashMap.put("总访问量", 1052d);
        hashMap.put("单日访问量", 452d);
        hashMap.put("连续访问最长时间", 100d);
        hashMap.put("未访问最长时间", 50d);
        return hashMap;
    }

    /**
     * 查询得分
     *
     * @param proId 得分
     * @return 得分
     */
    @Override
    public Double selectScoreList(Long proId) {
        HashMap<String, Double> hashMap = selectCloudResources();
        //  查询 指标与对应的规则
        int j = 0;
        List<PerformanceIndicators> list1 = performanceService.selectValidPerformanceRule();
        if(Collections.isEmpty(list1)){
            return null;
        }
        Double sum = 0d;
        for (PerformanceIndicators indicators : list1) {
            System.err.println("第 " + ++j + " 条指标：" + indicators);
            String name = indicators.getIndicatorsName();
            System.err.println("指标名称：" + name);
            System.err.println("name:" + hashMap.get(name));
            Double value = hashMap.get(name);
            int x = 0;
            if (indicators.getPerformanceRules() == null){
                continue;
            }
            for (InsertPerformanceRuleDto rule : indicators.getPerformanceRules()) {
                System.err.println("规则：" + ++x + "-->  " + rule);
                //rule.getRegulation() 条件(0:>，1:≥，2:=，3:≤，4:＜)
                // 规则成立 跳出循环
                Double valueOf = Double.valueOf(rule.getValue());
                if (rule.getRegulation().equals("0") && value > valueOf) {
                        sum += rule.getScore() * indicators.getIndicatorsWeight();
                        break;
                }else if (rule.getRegulation().equals("1") && value >= valueOf){
                    sum += rule.getScore() * indicators.getIndicatorsWeight();
                    break;
                }else if (rule.getRegulation().equals("2") && value.equals(valueOf)){
                    sum += rule.getScore() * indicators.getIndicatorsWeight();
                    break;
                }else if (rule.getRegulation().equals("3") && value <= valueOf){
                    sum += rule.getScore() * indicators.getIndicatorsWeight();
                    break;
                }else if (rule.getRegulation().equals("4") && value < valueOf){
                    sum += rule.getScore() * indicators.getIndicatorsWeight();
                    break;
                }
            }
        }
        System.err.println("总的 sum:"+sum);
        //计算总得分
        return sum;
    }

    @Override
    public BigDecimal selectAverageScore(List<Long> proIds) {
        return scoreMapper.selectAverageScore(proIds).setScale(2,BigDecimal.ROUND_HALF_UP);
    }

}
